package com.lin.missyou.repository;

import com.lin.missyou.model.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author gcq
 * @Create 2021-11-15
 */
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {

    /**
     * 查询条件
     * status = unpaid
     * expiredTime > now
     */
    Page<Order> findByStatusAndExpiredTimeGreaterThanAndUserId(int status, Date now, Long userId, Pageable pageable);

    Page<Order> findByUserId(Long uid, Pageable pageable);

    Page<Order> findByUserIdAndStatus(Long uid, Integer status, Pageable pageable);

    Optional<Order> findFirstByUserIdAndId(Long uid, Long oid);

    Optional<Order> findFirstByOrderNo(Long oid);

    @Modifying
    @Query("update Order o set o.status = :status where o.orderNo = :orderNo")
    int updateStatusByOrderNo(String orderNo, Integer status);

}